<template>
  <s-layout title="待支付订单">

    <view class="card-wrap">
      <view class="ss-flex">
        <image src="@/static/img/inheritanceOfficer-1.png" mode=""></image>
        <view class="card-wrap-t">
          <view class="card-wrap-t1">汽车减震器</view>
          <view class="ss-flex">
            <view class="card-wrap-t2">藏品售价：</view>
            <view class="card-wrap-t3">￥550.00</view>
          </view>
        </view>
      </view>
    </view>

    <view class="choose-wrap">
      <view class="choose-wrap-t ss-flex ss-row-between">
        <view class="choose-wrap-t1">已锁单藏品</view>
        <view class="ss-flex">
          <view class="choose-wrap-t2">全选</view>
        </view>
      </view>
      <view class="choose-wrap-l">
        <checkbox-group >
          <view class="choose-wrap-l-item ss-flex ss-row-between">
            <view>17970/50000</view>
            <checkbox activeColor="#45C9F0" iconColor="#000" label="绿色"></checkbox>
          </view>
          <view class="choose-wrap-l-item ss-flex ss-row-between">
            <view>17970/50000</view>
            <checkbox activeColor="#45C9F0" iconColor="#000" label="绿色"></checkbox>
          </view>
        </checkbox-group>
      </view>
    </view>

    <!-- 底部 -->
    <su-fixed bottom :opacity="false" bg="" placeholder :noFixed="false" :index="200">
      <view class="footer-box border-top ss-flex ss-row-between ss-p-x-20 ss-col-center">
        <view class="total-box-footer ss-flex ss-col-center">
          <view class="total-num ss-font-30 text-red"> ￥{{ state.orderInfo.pay_fee }}</view>
          <view v-if="state.orderPayload.order_type === 'score'" class="ss-flex">
            <view class="total-num ss-font-30 text-red ss-m-l-4">+</view>
            <image :src="sheep.$url.static('/assets/addons/shopro/uniapp/goods/score1.svg')"
                   class="score-img"></image>
            <view class="total-num ss-font-30 text-red">{{ state.orderInfo.score_amount }}</view>
          </view>
        </view>

        <button class="ss-reset-button ui-BG-Main-Gradient ss-r-40 submit-btn ui-Shadow-Main" @tap="onConfirm">
          {{ exchangeNow ? '立即兑换' : '提交订单' }}
        </button>
      </view>
    </su-fixed>
  </s-layout>
</template>

<script setup>
  import { computed, reactive, ref } from 'vue';
  import { onLoad } from '@dcloudio/uni-app';
  import sheep from '@/sheep';
  import { isEmpty } from 'lodash';

  const state = reactive({
    orderPayload: {},
    orderInfo: {},
    addressInfo: {},
    invoiceInfo: {},
    totalNumber: 0,
    showCoupon: false,
    couponInfo: [],
    showDiscount: false,
  });
  const checkboxValue1 = ref(false);
  // 立即兑换(立即兑换无需跳转收银台)
  const exchangeNow = computed(
    () => state.orderPayload.order_type === 'score' && state.orderInfo.pay_fee == 0,
  );

  // 选择地址
  function onSelectAddress() {
    uni.$once('SELECT_ADDRESS', (e) => {
      changeConsignee(e.addressInfo);
    });
    sheep.$router.go('/pages/user/address/list');
  }

  // 更改收货人地址&计算订单信息
  async function changeConsignee(addressInfo = {}) {
    if (isEmpty(addressInfo)) {
      const {
        code,
        data,
      } = await sheep.$api.user.address.default();
      if (code === 1 && !isEmpty(data)) {
        addressInfo = data;
      }
    }
    if (!isEmpty(addressInfo)) {
      state.addressInfo = addressInfo;
      state.orderPayload.address_id = state.addressInfo.id;
    }
    getOrderInfo();
  }

  // 选择优惠券
  async function onSelectCoupon(e) {
    state.orderPayload.coupon_id = e || 0;
    getOrderInfo();
    state.showCoupon = false;
  }

  // 选择发票信息
  function onSelectInvoice() {
    uni.$once('SELECT_INVOICE', (e) => {
      state.invoiceInfo = e.invoiceInfo;
      state.orderPayload.invoice_id = e.invoiceInfo.id || 0;
    });
    sheep.$router.go('/pages/user/invoice/list');
  }

  // 提交订单/立即兑换
  function onConfirm() {
    if (!state.orderPayload.address_id && state.orderInfo.need_address === 1) {
      sheep.$helper.toast('请选择收货地址');
      return;
    }

    if (exchangeNow.value) {
      uni.showModal({
        title: '提示',
        content: '确定使用积分立即兑换?',
        cancelText: '再想想',
        success: async function(res) {
          if (res.confirm) {
            submitOrder();
          }
        },
      });
    } else {
      submitOrder();
    }
  }

  // 创建订单&跳转
  async function submitOrder() {
    const {
      code,
      data,
    } = await sheep.$api.order.create(state.orderPayload);
    if (code === 1) {
      // 更新购物车列表
      if (state.orderPayload.from === 'cart') {
        sheep.$store('cart').getList();
      }
      if (data.status === 'paid') {
        sheep.$router.redirect('/pages/pay/result', {
          orderSN: data.order_sn,
        });
      } else {
        sheep.$router.redirect('/pages/pay/index', {
          orderSN: data.order_sn,
        });
      }
    }
  }

  // 检查库存&计算订单价格
  async function getOrderInfo() {
    let {
      code,
      data,
    } = await sheep.$api.order.calc(state.orderPayload);
    if (code === 1) {
      state.totalNumber = 0;
      state.orderInfo = data;
      state.orderInfo.goods_list.forEach((item) => {
        state.totalNumber += item.goods_num;
      });
    }
  }

  // 获取可用优惠券
  async function getCoupons() {
    const {
      code,
      data,
    } = await sheep.$api.order.coupons(state.orderPayload);
    if (code === 1) {
      state.couponInfo = data;
    }
  }

  onLoad(async (options) => {
    // if (options.data) {
    // 	state.orderPayload = JSON.parse(options.data);
    // 	changeConsignee();
    // 	if (state.orderPayload.order_type !== 'score') {
    // 		getCoupons();
    // 	}
    // }
  });
</script>

<style lang="scss" scoped>
  .card-wrap {
    margin: 30rpx;
    padding: 20rpx;
    background: #1E1E1E;
    border-radius: 20rpx;

    image {
      width: 180rpx;
      height: 176rpx;
      margin-right: 35rpx;
    }

    .card-wrap-t {
      .ss-flex {
        align-items: baseline;
      }

      .card-wrap-t1 {
        margin-bottom: 34rpx;

        font-weight: 400;
        font-size: 28rpx;
        color: #FFFFFF;
      }

      .card-wrap-t2 {

        font-weight: 400;
        font-size: 24rpx;
        color: #45C9F0;
      }

      .card-wrap-t3 {

        font-weight: 400;
        font-size: 36rpx;
        color: #45C9F0;
      }
    }
  }

  .choose-wrap {
    margin: 30rpx;

    .choose-wrap-t {
      width: 100%;

      .choose-wrap-t1 {

        font-weight: 400;
        font-size: 32rpx;
        color: #FFFFFF;
      }

      .choose-wrap-t2 {

        font-weight: 400;
        font-size: 30rpx;
        color: #FFFFFF;
      }
    }

    .choose-wrap-l {
      .choose-wrap-l-item {
        background: #1E1E1E;
        border-radius: 20rpx;
        padding: 26rpx;
        margin-top: 22rpx;

        font-weight: 400;
        font-size: 28rpx;
        color: #FFFFFF;
      }
    }
  }
</style>
